/*
 * =====================================================================================
 *
 *       Filename:  sar_tool.c
 *
 *    Description:  tools using sar
 *
 *        Version:  1.0
 *        Created:  2013年11月15日 19时57分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BeeSui Xue (BX), xuejilong@foxmail.com
 *        Website:  http://beesui.com
 *        License:  GNU General Public License
 *   Organization:  Free Person
 *
 * =====================================================================================
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

//获取cpu使用率
int get_cpu_usage(int step, int total_steps, const char * filepath)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "export LC_ALL=C;sar -u %d %d > %s", step, total_steps, filepath);
	return execute_cmd(cmd, result);
}

//获取网络使用率
int get_network_usage(int step, int total_steps, const char * filepath)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "export LC_ALL=C;sar -n DEV %d %d > %s", step, total_steps, filepath);
	return execute_cmd(cmd, result);
}

//获取磁盘使用率
int get_disk_usage(int step, int total_steps, const char * filepath)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "export LC_ALL=C;sar -d %d %d > %s", step, total_steps, filepath);
	return execute_cmd(cmd, result);
}

//获取内存使用率
int get_memory_usage(int step, int total_steps, const char * filepath)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "export LC_ALL=C;sar -r %d %d > %s", step, total_steps, filepath);
	return execute_cmd(cmd, result);
}

//将输入的文件制作成jpg文件
int gen_jpg_file(const char *input_file, const char *output_file)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "java -jar kSar.jar -input %s -outputJPG %s", input_file, output_file);
	return execute_cmd(cmd, result);
}

//将输入的文件制作成png文件
int gen_png_file(const char *input_file, const char *output_file)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "java -jar kSar.jar -input %s -outputPNG %s", input_file, output_file);
	return execute_cmd(cmd, result);
}

//将输入的文件制作成csv文件
int gen_csv_file(const char *input_file, const char *output_file)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "java -jar kSar.jar -input %s -outputCSV %s", input_file, output_file);
	return execute_cmd(cmd, result);
}

//将输入的文件制作成pdf文件
int gen_pdf_file(const char *input_file, const char *output_file)
{
	char cmd[1024];
	char result[1024];
	sprintf(cmd, "java -jar kSar.jar -input %s -outputPDF %s", input_file, output_file);
	return execute_cmd(cmd, result);
}
